package com.fujifilm.project.plan.service.impl;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.fujifilm.project.plan.error.BusinessException;
import com.fujifilm.project.plan.security.JwtUtils;
import com.fujifilm.project.plan.security.UserImplDetails;
import com.fujifilm.project.plan.service.IUserService;
import com.fujifilm.project.plan.vo.request.RequestRecords.UserLoginRequest;
import com.fujifilm.project.plan.vo.response.ResponseRecords.UserInfoResponse;
import com.fujifilm.project.plan.vo.response.ResponseRecords.UserLoginResponse;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 *
 *
 * @date 2025年6月05日
 * @time 22:26:01
 * @description
 *
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserServiceImpl implements IUserService {
    private final AuthenticationManager authenticationManager;
    private final JwtUtils jwtUtils;

    @Override
    public UserLoginResponse login(UserLoginRequest req) throws AuthenticationException {
        Authentication authentication = authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        req.username(),
                        req.password()));
        UserImplDetails userDetails = (UserImplDetails) authentication.getPrincipal();
        String jwtToken = jwtUtils.generateToken(userDetails);
        return new UserLoginResponse(userDetails.getId(), jwtToken);
    }

    @Override
    public void logout() {
        log.info("Logout called, but no action is needed for JWT-based authentication.");
        // 如果需要实现登出逻辑，可以考虑将令牌加入黑名单或其他方式
        // 但这通常不是必要的，因为JWT令牌是无状态的。
        // 例如，可以在数据库中记录已登出的令牌，或使用缓存来存储黑名单。
        // 这里可以添加登出逻辑，比如将令牌加入黑名单等
        // 目前不需要实现登出逻辑，JWT令牌在客户端存储，服务端不需要维护会话状态
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.isAuthenticated()) {
            log.info("User {} logged out successfully.", authentication.getName());
        }

    }

    @Override
    public UserInfoResponse info() throws BusinessException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new BusinessException("User not authenticated");
        }

        UserImplDetails userDetails = (UserImplDetails) authentication.getPrincipal();
        if (userDetails == null) {
            log.error("User details not found in the authentication context.");
            throw new BusinessException("User not authenticated");
        }
        return new UserInfoResponse(userDetails.getId(),
                userDetails.getUsername(),
                userDetails.getEmail(),
                userDetails.getRealName());
    }

}
